//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 
    discountId: 0,
    // modal0,modal1
    hiddenmodal0: true,
    hiddenmodal1: true,
    hiddenmodal2: true,
    // 真实姓名，身份证号
    trueName: "",
    IDNum: "",
    carNum: "",
    // 付款
    payFlag: true,
    orderNumber: "",
    payType: "",
    code: "",
    // 会员信息
    vipInfo: [],
    // 
    uid: "",
    userType: "",
    // 输入支付密码
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var discountId = this.data.discountId;
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
      discountId: options.discountId
    })
    this.BuluoAreaMemberInfo();
  },


  //显示支付密码输入层
  showInputLayer: function() {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function() {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: ''
    });

  },
  // 获取焦点
  getFocus: function() {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function(e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //输入密码 确定 
  surePasswords: function() {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=BuluoAreaOrderPay&UID=" + uid + "&OrderNumber=" + orderNumber + "&PayType=1"),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data))
              console.log(json);
              if (json.ResultFlag == "1") {
                that.BuluoAreaMemberInfo();
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // 付费续费0，激活码付费续费1
  payTap: function(e) {
    var that = this;
    var discountId = this.data.discountId;
    var payStr = e.currentTarget.dataset.paystr;
    this.setData({
      payStr: payStr
    })
    if (discountId == 3) {
      if (payStr == "money") {
        this.setData({
          payFlag: false
        })
      } else {
        this.setData({
          hiddenmodal2: false,
        })
      }

    } else {
      this.setData({
        hiddenmodal0: !this.data.hiddenmodal0,
      })
    }

  },
  // 真实姓名
  trueNameTap: function(e) {
    var that = this;
    var trueName = this.data.trueName;
    this.setData({
      trueName: e.detail.value
    });

  },
  // 身份证号
  IDNumTap: function(e) {
    var that = this;
    var IDNum = this.data.IDNum;
    this.setData({
      IDNum: e.detail.value
    });
  },
  // 车牌号
  carNumTap: function(e) {
    var that = this;
    var carNum = this.data.carNum;
    this.setData({
      carNum: e.detail.value
    });

  },
  // 激活码
  codeTap: function(e) {
    var that = this;
    var code = this.data.code;
    this.setData({
      code: e.detail.value
    });
  },
  //modal0取消，确定
  modal0Cancel: function() {
    this.setData({
      hiddenmodal0: true
    });
  },
  modal0Confirm: function() {
    this.setData({
      hiddenmodal0: true,
      hiddenmodal1: false
    });
  },
  //modal1取消，确定
  modal1Cancel: function() {
    this.setData({
      hiddenmodal1: true
    });
  },
  modal1Confirm: function() {
    this.setData({
      hiddenmodal1: true,
    })
    this.createOrder();
  },
  //modal1取消，确定
  modal2Cancel: function() {
    this.setData({
      hiddenmodal2: true
    });
  },
  modal2Confirm: function() {
    // 激活码支付
    this.setData({
      hiddenmodal2: true,
    })
    var that = this;
    var uid = this.data.uid;
    var code = this.data.code;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=BuluoAreaOrderPay&UID=" + uid + "&OrderNumber=" + orderNumber + "&PayType=4" + "&MemberCard=" + code),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 1000
          });
          that.BuluoAreaMemberInfo();
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }

      }
    })
  },
  // 生成订单
  createOrder: function() {
    var that = this;
    var uid = this.data.uid;
    var discountId = this.data.discountId;
    var trueName = this.data.trueName;
    var IDNum = this.data.IDNum;
    var carNum = this.data.carNum;
    // 钱或激活卡
    var hiddenmodal2 = this.data.hiddenmodal2;
    var payFlag = this.data.payFlag;
    var payStr = this.data.payStr;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CreateBuluoAreaOrder&AreaId=" + discountId + "&UID=" + uid + "&TrueName=" + trueName + "&IdNumber=" + IDNum + "&CarNumber=" + carNum),
      success: function(res) {
        console.log("method=CreateBuluoAreaOrder&AreaId=" + discountId + "&UID=" + uid + "&TrueName=" + trueName + "&IdNumber=" + IDNum + "&CarNumber=" + carNum);
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == "1" && payStr == "money") {
          orderNumber = json.ResultData;
          that.setData({
            payFlag: false,
            orderNumber: orderNumber
          })
          console.log("钱");
        } else if (json.ResultFlag == "1" && payStr == "code") {
          // 激活卡
          console.log("激活码");
          orderNumber = json.ResultData;
          that.setData({
            hiddenmodal2: false,
            orderNumber: orderNumber
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })

  },
  // 取消付款
  cancelPay: function() {
    this.setData({
      payFlag: true
    })
  },
  //余额或微信 
  radioChange: function(e) {
    var payType = this.data.payType;
    payType = e.detail.value
    this.setData({
      payType: payType,
    });
  },
  // 付款
  goPay: function() {
    var that = this;
    var uid = this.data.uid;
    var payType = this.data.payType;
    var orderNumber = this.data.orderNumber;
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    if (payType == '') {
      wx.showToast({
        title: "请选择支付方式！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    this.setData({
      payFlag: true
    })
    if (payType == "1") {
      // 余额
      if (isAlreadyPayPassword == '1') {
        that.setData({
          showPayPwdInput: true,
          payFocus: true,
        })
      } else {
        wx.showToast({
          title: "请先去设置支付密码！",
          icon: 'none',
          duration: 2000
        });
      }
    } else {
      wx.request({
        url: baseJs.api + baseJs.encryUrl("method=BuluoAreaOrderPay&UID=" + uid + "&OrderNumber=" + orderNumber + "&PayType=2"),
        success: function(res) {
          var json = JSON.parse(baseJs.decryUrl(res.data));
          console.log(json);
          if (json.ResultFlag == 1) {
            // wx.requestPayment({
            //   timeStamp: json.ResultData.timestamp,
            //   nonceStr: json.ResultData.nonce_str,
            //   package: 'prepay_id=' + json.ResultData.prepay_id,
            //   signType: 'MD5',
            //   paySign: json.ResultData.sign,
            //   success(res) {
            //     console.log(res);
            //     that.BuluoAreaMemberInfo();
            //   },
            //   fail(res) {
            //     console.log(res);
            //     wx.showToast({
            //       title: res.err_desc,
            //       icon: 'none',
            //       duration: 2000
            //     });
            //   }
            // })
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: 'none',
              duration: 2000
            });
          }

        }
      })
    }
  },
  // 会员信息
  BuluoAreaMemberInfo: function() {
    var that = this;
    var uid = this.data.uid;
    var discountId = this.data.discountId;
    var vipInfo = this.data.vipInfo;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=BuluoAreaMemberInfo&AreaId=" + discountId + "&UID=" + uid),
      success: function(res) {
        vipInfo = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(vipInfo);
        that.setData({
          vipInfo: vipInfo,
        })
      }
    })
  },
  // 
})